﻿Public Class COAResultWizard
    'Public ItemMasterDT As DataTable
    'Public aPL As PLSDK.PL.Void_PL

    'COAResultWizard.vb

    Public aItemMasterPL As SystemBoxSDK.PL.MESSDK.ItemMaster_PL
    Public WithEvents aGridItemMaster As GridExtPL
    Public selectedItemMasterRow As DataRow
    Public selectedItemMasterID As String

    Public aSpecPL As SystemBoxSDK.PL.MESSDK.ItemMasterSpecification_PL
    Public WithEvents aGridSpec As GridExtPL
    Public selectedSpecList As List(Of Guid) = New List(Of Guid)

    Public aClientSpecPL As SystemBoxSDK.PL.MESSDK.ClientSpecification_PL
    Public WithEvents aGridClientSpec As GridExtPL

    Public aSocietyPL As SystemBoxSDK.PL.CoreSDK.Society_PL
    Public WithEvents aGridSociety As GridExtPL


    Public Overridable Sub WizardControl1_CancelClick(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WizardControl1.CancelClick
        If MessageBox.Show("Do you want to exit ?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If

    End Sub

    Public Overridable Sub WizardControl1_FinishClick(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WizardControl1.FinishClick
        Me.Close()
    End Sub

    Public Overridable Sub WizardControl1_SelectedPageChanged(sender As Object, e As DevExpress.XtraWizard.WizardPageChangedEventArgs) Handles WizardControl1.SelectedPageChanged
        Select Case e.Page.Name
            Case "SelectItemMaster"
                If aItemMasterPL Is Nothing Then

                    aItemMasterPL = New SystemBoxSDK.PL.MESSDK.ItemMaster_PL("COA_ItemMaster")
                    aGridItemMaster = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = aItemMasterPL, .LoadWithChilds = False}
                    aGridItemMaster.Name = "GridLatestItemMaster"
                    aGridItemMaster.AllowCreate = False
                    aGridItemMaster.AllowDelete = False
                    aGridItemMaster.AllowUpdate = False
                    aGridItemMaster.SetListlike()

                    e.Page.Controls.Add(aGridItemMaster)

                    AddHandler aGridItemMaster.MasterRowClick, AddressOf ItemMasterRowClick

                End If

            Case "SelectSpec"
                If selectedItemMasterRow IsNot Nothing AndAlso selectedItemMasterRow("ItemMasterRevID").ToString() <> selectedItemMasterID Then
                    'ClearSummary()

                    selectedItemMasterID = selectedItemMasterRow("ItemMasterRevID").ToString()
                    'If aSpecPL IsNot Nothing Then
                    '    aSpecPL.Dispose()
                    'End If

                    aSpecPL = New SystemBoxSDK.PL.MESSDK.ItemMasterSpecification_PL("COA_ItemMasterSpecification")
                    aSpecPL.SQLWhere = String.Format("ItemMasterRevID = '{0}'", selectedItemMasterID)
                    aGridSpec = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = aSpecPL, .LoadWithChilds = False, .SecureGrid = False}
                    aGridSpec.Name = "GridItemMasterSpec"
                    aGridSpec.AllowCreate = False
                    aGridSpec.AllowDelete = False


                    AddHandler aGridSpec.MasterCellValueChanged, AddressOf Spec_CellValueChanged

                    e.Page.Controls.Remove(e.Page.Controls("GridItemMasterSpec"))

                    e.Page.Controls.Add(aGridSpec)
                End If

            Case "ClientSpecPage"
                If aClientSpecPL Is Nothing OrElse (selectedItemMasterRow IsNot Nothing AndAlso selectedItemMasterRow("ItemMasterRevID").ToString() <> selectedItemMasterID) Then

                    'If aClientSpecPL IsNot Nothing Then
                    '    aClientSpecPL.Dispose()
                    'End If

                    Dim aGuid As Guid
                    Dim strListOfGuid As String = String.Empty
                    For Each aGuid In selectedSpecList
                        strListOfGuid = strListOfGuid + "'" + aGuid.ToString() + "',"
                    Next
                    strListOfGuid = strListOfGuid.Remove(strListOfGuid.Length - 1)


                    aClientSpecPL = New SystemBoxSDK.PL.MESSDK.ClientSpecification_PL("COA_ClientSpecification")
                    aClientSpecPL.SQLWhere = String.Format("IMSRD.ItemMasterSpecRevDetailID in ({0})", strListOfGuid)
                    aGridClientSpec = Nothing
                    aGridClientSpec = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = aClientSpecPL, .LoadWithChilds = False, .SecureGrid = False}
                    aGridClientSpec.Name = "GridClientSpec"
                    aGridClientSpec.AllowCreate = False
                    aGridClientSpec.AllowDelete = False


                    'AddHandler aGridSpec.MasterCellValueChanged, AddressOf Spec_CellValueChanged

                    'e.Page.Controls.Remove(e.Page.Controls("GridClientSpec"))
                    e.Page.Controls.Add(aGridClientSpec)
                End If

            Case "SelectClient"
                If aSocietyPL Is Nothing Then
                    aSocietyPL = New SystemBoxSDK.PL.CoreSDK.Society_PL("COA_Society")
                    'aSpecPL.SQLWhere = String.Format("ItemMasterRevID = '{0}'", selectedItemMasterID)
                    aGridSociety = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = aSocietyPL, .LoadWithChilds = False, .SecureGrid = False}
                    aGridSpec.Name = "GridSociety"
                    aGridSociety.AllowCreate = False
                    aGridSociety.AllowDelete = False

                    e.Page.Controls.Add(aGridSociety)
                End If

            Case "ProgressPage"
                SaveData()


        End Select
    End Sub

    Public Overridable Sub SetWizardPage(sender As System.Object, e As DevExpress.XtraWizard.WizardPageChangingEventArgs) Handles WizardControl1.SelectedPageChanging

        Select Case e.Page.Name
            Case "SelectItemMaster"

                'aItemMasterPL = New PLSDK.PL.MESSDK.ItemMaster_PL("COA_ItemMaster")
                'Dim aGridControl As GridExtPL = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = aPL}
                'aGridControl.Name = "GridListLatestItemMaster"
                'e.Page.Controls.Add(aGridControl)

            Case ""


        End Select


    End Sub


    Public Overridable Sub SaveData()
        progressBar.Properties.Minimum = 0
        progressBar.Properties.Maximum = 100
        progressBar.Properties.Step = 10
        progressBar.Properties.ShowTitle = True
        progressBar.Properties.PercentView = True
        progressBar.Properties.Appearance.ForeColor = Color.White
        progressBar.Position = progressBar.Properties.Minimum
        RunVariableIncrement()

        'Dim aRow As DataRow
        'Dim aRow2 As DataRow
        'For Each aRow In aGridSociety.Rows
        '    For Each aRow2 In aGridClientSpec.Rows
        '        aRow2("SocietyId_FK") = DirectCast(aRow("SocietyId"), Guid)
        '    Next
        '    aClientSpecPL.Save()
        'Next

    End Sub

    Public Overridable Sub RunVariableIncrement()
        progressBar.Position = progressBar.Properties.Minimum
        'progressBarSample2.Position = progressBarSample2.Properties.Minimum;

        Dim currentStep As Double = (progressBar.Properties.Maximum - progressBar.Properties.Minimum) / 4
        'double currentStep = (progressBarSample2.Properties.Maximum - progressBarSample2.Properties.Minimum) / 4;

        While progressBar.Position + currentStep < progressBar.Properties.Maximum
            progressBar.Increment(CInt(Math.Ceiling(currentStep)))
            progressBar.Update()
            System.Threading.Thread.Sleep(100)
            currentStep /= 1.5

        End While

        'while(progressBarSample2.Position + currentStep < progressBarSample2.Properties.Maximum) {
        '     progressBarSample2.Increment((int)Math.Ceiling(currentStep));
        '     progressBarSample2.Update();
        '     textEditCurrentValue.Update();
        '     System.Threading.Thread.Sleep(100);
        '     currentStep /= 1.5;
    End Sub



    ''' <summary>
    ''' This sub save the selected ItemMaster everytime it changes so it can be use to filter ItemMaster's Specs in the next step
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub ItemMasterRowClick(ByVal sender As Object, ByVal e As RowClickEventArgs)
        Dim view As GridView = CType(sender, GridView)
        selectedItemMasterRow = view.GetFocusedDataRow()
    End Sub


    ''' <summary>
    ''' This sub add/remove from a list, witch will be used to save the desired test
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub Spec_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs)
        Dim view As GridView = CType(sender, GridView)
        Dim aRow As DataRow = view.GetFocusedDataRow()
        Dim aGuid As Guid = DirectCast(aRow("ItemMasterSpecRevDetailID"), Guid)

        If e.Column.FieldName = "Selected" Then
            If CBool(e.Value.ToString()) = True Then
                If selectedSpecList.Contains(aGuid) = False Then
                    selectedSpecList.Add(aGuid)
                End If
            ElseIf CBool(e.Value.ToString()) = False Then
                If selectedSpecList.Contains(DirectCast(aRow("ItemMasterSpecRevDetailID"), Guid)) = True Then
                    selectedSpecList.Remove(aGuid)
                End If
            End If
        End If
    End Sub
End Class